package com.mt.fpb.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.fpb.enums.ResponseCode;
import com.mt.fpb.mapper.UserMapper;
import com.mt.fpb.mapper.UserWxUserMapper;
import com.mt.fpb.model.User;
import com.mt.fpb.model.UserWxUser;
import com.mt.fpb.model.vo.CommonResult;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("ruixiang/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserWxUserMapper userWxUserMapper;


    /**
     * 用于个人中心页面的登录
     * @Author yu
     * @date 2021/8/10 10:15
     * @Param [phoneNumber, openId]
     * @return com.mt.fpb.model.vo.CommonResult
     **/

    @PostMapping("login")
    public CommonResult login(User user,String openId){
        System.out.println("用户名"+user.getUserName()+"手机号"+user.getPhoneNumber()+user.getPartner()+user.getCName());
        System.out.println("openId:"+openId);
        //设置查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        //通过用户名或手机号进行查询
        queryWrapper.eq("user_name",user.getUserName()).or().
                eq("phone_number",user.getPhoneNumber());
        //得到查询结果
        User u = userMapper.selectOne(queryWrapper);
        System.out.println("u:"+u);
        if(u != null){
            //执行更新操作，更新user表中的数据
            System.out.println("user:"+user);
            userMapper.update(user,queryWrapper);
            //将查询出来的用户和当前登录的微信用户的openId进行绑定
            UserWxUser userWxUser = new UserWxUser();
            userWxUser.setUserId(u.getUserId());
            userWxUser.setOpenId(openId);

//            int  userId = u.getUserId();
//            QueryWrapper qw = new QueryWrapper();
//            qw.eq("user_id",userId);
//            userWxUserMapper.selectOne(qw);
            //查询绑定表中是否有绑定数据，
            QueryWrapper q = new QueryWrapper();
            q.eq("user_id",u.getUserId());
            q.eq("open_id",openId);
            userWxUserMapper.delete(q);
            System.out.println("删除");
            userWxUserMapper.insert(userWxUser);
            //返回查询出来的用户信息
            return CommonResult.success(u.getUserId());

        }else{

            //执行插入操作 插入数据到user表中
            int result = userMapper.insert(user);
            if(result>0){
                //前端要登录的用户，数据库中没有，所以在插入成功后通过当前的username去获取userId，然后在和openId绑定
                QueryWrapper<User> queryUser = new QueryWrapper<User>();
                queryUser.eq("user_name",user.getUserName());
                User  currentUser = userMapper.selectOne(queryUser);
                System.out.println("currentUser:"+currentUser);
                //通过userId和openId绑定 即向user_wxuser表中插入数据
                UserWxUser userWxUser = new UserWxUser();
                userWxUser.setUserId(currentUser.getUserId());
                userWxUser.setOpenId(openId);
                userWxUserMapper.insert(userWxUser);
                //返回新增的用户的userId
                return CommonResult.success(user.getUserId());
            }
        }
        return CommonResult.fail(ResponseCode.FAILED);
    }

    /**
     *通过userId获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("getUserInfo")
    public CommonResult getUserInfo( int userId){
        //通过前页面传过来的userId查询用户信息
        User user = userMapper.selectById(userId);
        if(user != null){
            return CommonResult.success(user);
        }
        return CommonResult.fail(ResponseCode.FAILED);

    }

    /***
     * 登出接口
     * @param userId
     * @param openId
     * @return
     */
    @GetMapping("logout")
    public CommonResult logout(int userId,String openId){

        QueryWrapper<UserWxUser> query = new QueryWrapper<UserWxUser>();
//        query.eq("user_id",userWxUser.getUserId());
//        query.eq("open_id",userWxUser.getOpenId());
        query.eq("user_id",userId);
        query.eq("open_id",openId);
        int result = userWxUserMapper.delete(query);
        if(result>0){
            return CommonResult.success(ResponseCode.LOGOUT);
        }else{
            return CommonResult.fail(ResponseCode.FAILED);
        }

    }

    /**
     * 通过微信的openId获取用户信息
     * @param openId
     * @return user
     */
    @GetMapping("getUserByOpenId")
    public CommonResult getUserByOpenId(String openId){
        User user = userMapper.getUserInfoByOpenId(openId);
        if(user !=null){
            return CommonResult.success(user);
        }
        return CommonResult.fail(ResponseCode.FAILED);
    }











}
